﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Deployment.Application;

namespace StaticPlugin
{
    class jUpdater : StaticPlugin.AJob
    {
        private string status = "Idle";
        public TimeSpan tsUpdatesCheckPeriod = TimeSpan.FromSeconds(20.0);
        ApplicationDeployment ad = null;
        int imax = 60;

        protected override string getName()
        {
            return "Автоматическое обновление";
        }

        protected override string getStatus()
        {
            return status;
        }

        protected override bool getAvailable()
        {
            return true;
        }

        protected override int getPriority()
        {
            return -1;
        }

        int i = 0;

        public override bool testRequirements(WebBrowser browser)
        {
            UpdateCheckInfo info = null;

            try
            {
                ad = ApplicationDeployment.CurrentDeployment;
            }
            catch (InvalidDeploymentException ex)
            {
                status = "Error in get deployment: " + ex.Message;
                return false;
            }

            i++;

            if (i < imax)
            {
                status = "Вермя обновления еще не подошло. Tlast=" + (imax - i).ToString();
                return false;
            }

            i = 0;

            try
            {
                info = ad.CheckForDetailedUpdate();
            }
            catch (DeploymentDownloadException dde)
            {
                status = ("The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " + dde.Message);
                return false;
            }
            catch (InvalidDeploymentException ide)
            {
                status = ("Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " + ide.Message);
                return false;
            }
            catch (InvalidOperationException ioe)
            {
                status = ("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
                return false;;
            }

            if (info.UpdateAvailable) 
            {
                status = "Требуется обновление!";
                return true;
            }
            status = "Нет доступных обновлений ";
            return false;
        }

        public override DialogResult showSetupDialog()
        {
            return DialogResult.Cancel;
        }

        public override void initActions()
        {
            actions.Clear();
            actions.Add(new Action("", DownloadAndInstallUpdate));
        }


        public void DownloadAndInstallUpdate(WebBrowser b) 
        {
                try
                {
                    ad.Update();
                    Application.Restart();
                }
                catch (DeploymentDownloadException dde)
                {
                    status = ("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
                    return;
                }
        }

    }
}
